using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using System.Net.Mail;
using System.Net;
/// <summary>
/// Summary description for Email
/// </summary>
public class Email
{
    private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
	public Email()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static void doSendMail(String Subject,String listToEmail,String content, bool isFormatHTML)
    {
        MailMessage mail = new MailMessage();
        String[] mailList = listToEmail.Split(';');

        foreach (string email in mailList)
        {
            if (!email.Equals(""))
            {
                try
                {
                    mail.To.Add(email);
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                }
            }
        }

        try
        {
            mail.Subject = Subject;
            mail.From = new MailAddress(Config.EMAIL_SENDER);
            mail.Body = content;
            mail.IsBodyHtml = isFormatHTML;
            
            SmtpClient smtpClient = new SmtpClient();
            
            smtpClient.Host = Config.SMTP_HOST;
            smtpClient.Port = Convert.ToInt32(Config.PORT); // neu thay doi smtp server thi thay doi lai port(25)
            smtpClient.EnableSsl = Convert.ToBoolean(Config.SSL);
            smtpClient.UseDefaultCredentials = true;
            //smtpClient.Credentials = new NetworkCredential(Config.SMTP_AUTH_USERNAME,Utils.Decrypt(Config.SMTP_AUTH_PASSWORD));
            smtpClient.Credentials = new NetworkCredential(Config.SMTP_AUTH_USERNAME, Config.SMTP_AUTH_PASSWORD);
            smtpClient.Send(mail);
        }
        catch (SmtpException se)
        {
            logger.Error(se.Message);
        }
    }
    public static void doSendSpamMail(String strFromMail, String Subject, String listToEmail, String content, bool isFormatHTML)
    {
        MailMessage mail = new MailMessage();
        String[] mailList = listToEmail.Split(';');

        foreach (string email in mailList)
        {
            if (!email.Equals(""))
            {
                try
                {
                    mail.To.Add(email);
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                }
            }
        }

        try
        {
            mail.Subject = Subject;
            mail.From = new MailAddress(strFromMail);
            mail.Body = content;
            mail.IsBodyHtml = isFormatHTML;

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = Config.SMTP_SPAM_HOST;
            smtpClient.Port = Convert.ToInt32(Config.PORT_SPAM); // neu thay doi smtp server thi thay doi lai port(25)
            smtpClient.EnableSsl = Convert.ToBoolean(Config.SSL_SPAM);
            smtpClient.Credentials = new NetworkCredential(Config.SMTP_SPAM_AUTH_USERNAME, Config.SMTP_SPAM_AUTH_PASSWORD);
            smtpClient.Send(mail);
        }
        catch (Exception ex)
        {
            logger.Error(ex.Message);
        }
    }

    public static void doSendMailCc(String Subject, String listToEmail, string FromEmail, string strEmailCc, String content, bool isFormatHTML)
    {
        MailMessage mail = new MailMessage();
        String[] mailList = listToEmail.Split(';');        
        foreach (string email in mailList)
        {
            if (!email.Equals(""))
            {
                try
                {
                    mail.To.Add(email);
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                }
            }
        }

        try
        {
            mail.CC.Add(strEmailCc);
            mail.Subject = Subject;
            mail.From = new MailAddress(FromEmail);
            mail.Body = content;
            mail.IsBodyHtml = isFormatHTML;

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.Host = Config.SMTP_HOST;
            smtpClient.Port = Convert.ToInt32(Config.PORT); // neu thay doi smtp server thi thay doi lai port(25)
            smtpClient.EnableSsl = Convert.ToBoolean(Config.SSL);
            smtpClient.UseDefaultCredentials = true;
            //smtpClient.Credentials = new NetworkCredential(Config.SMTP_AUTH_USERNAME,Utils.Decrypt(Config.SMTP_AUTH_PASSWORD));
            smtpClient.Credentials = new NetworkCredential(Config.SMTP_AUTH_USERNAME, Config.SMTP_AUTH_PASSWORD);
            smtpClient.Send(mail);
        }
        catch (SmtpException se)
        {
            logger.Error(se.Message);

        }
    }

}
